<?php

namespace App\Http\Controllers\Company;

use App\Facades\Site;
use App\Facades\Style;
use App\Http\Controllers\Company\CommonController;
use App\Models\Common\WebConfig;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyJob;
use App\Models\Company\Points;
use App\Models\News\News;
use App\Models\Personal\ResumeSend;
use App\Models\Company\CompanyUser;
use App\Models\Company\CrmService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class WelcomeController extends CommonController
{
    /**
     * 企业账户中心
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function index(Request $request)
    {
        $userInfo = auth()->guard('company')->user();
        //获取积分数
        $integralNumber = $userInfo->integralNumber();
        //扣除积分数
        $integralDeduct = $userInfo->integralDeduct();
        $points = Points::where('com_uid', $userInfo->uid)->orderBy('uptime', 'desc')->first(['uptime']);
        $uptime = !empty($points->uptime) ? substr($points->uptime, 0, 10) : '';
        $companyBasic = CompanyBasic::where('uid', $userInfo->uid)->orderBy('updatetime', 'desc')->first();
        //$company_basic->logo=url('/upload/companylogo/'.$company_basic->logo);
        if (empty($companyBasic->id)) {
            return redirect("/company/register_basic");
        }

//        更新非会员职位状态
        if (\Site::get('ordinaryJobLimit') == 1 && ($userInfo->viptime == null || $userInfo->viptime < date("Y-m-d H:i:s"))) {//将非会员职位设置成冻结中
            CompanyJob::where(['uid' => $userInfo->uid, 'job_status' => 1])->update(['job_status' => 7]);
        }else {//把冻结的职位变为公开
            CompanyJob::where(['uid' => $userInfo->uid, 'job_status' => 7])->update(['job_status' => 1]);
        }

        //获取全部发布中的职位
        $companyJob = CompanyJob::select('id', 'updatetime', 'com_id', 'recruiting_num', 'job_title', 'salary_above',
            'salary_below', 'pageviews', 'urgent')->where('uid', $userInfo->uid)
            ->where('job_status', 1)->get();
        foreach ($companyJob as $v) {
            //急招
            if ($v->urgent > date('Y-m-d H:i:s', strtotime('-5 day'))) {
                $v->iurgent = 1;
            } else {
                $v->iurgent = 0;
            }
        }
        $refreshtip = $request->refreshtip;//是否提示刷新职位
        if (count($companyJob) > 0) {
            if ($companyJob[0]->updatetime < date('Y-m-d H:i:s', strtotime('-2 day'))) {
                $refreshtip = 'yes';
            }
        }
        //收到简历数
        $resumeSendCount = ResumeSend::where('company_uid', $userInfo->uid)->where('status', '!=', 4)->count();
        //会员到期日期
        $endtime = $this->timeDiff($userInfo->viptime);

        //会员公告
        $vipNews = DB::table('news as t1')
            ->join('news_category as t2', 't1.category', '=', 't2.id')
            ->select('t1.id', 't1.title', 't2.new_sign')
            ->where('t2.new_sign', 'vip_announcement')
            ->orderBy('updated_at', 'desc')->take(3)->get();

        //查询客服人员
        $ServiceSql = CrmService::select('uid', 'sex', 'worker_name', 'weichat', 'qq', 'img', 'enounce', 'mobile');

        if (!empty($userInfo->crm_uid)) {
            $CrmService = $ServiceSql->where('uid', $userInfo->crm_uid)->first();//提取客服信息
        } else {
            $CrmService = $ServiceSql->where('uid', Site::get('DefaultCompanyService'))->first();
        }

        if (empty($CrmService)) $CrmService = new CrmService();
        //照片
        if (!empty(Site::get('CrmHttpAdress'))) {
            if (!empty($CrmService->img)) {
                $CrmService->img = Site::get('CrmHttpAdress') . '/upload/worker/' . $CrmService->img;
            } else {
                $CrmService->img = asset('default/images/company/headpic.jpg');
            }
        } else {
            if (!empty($CrmService->img)) {
                $CrmService->img = asset('/upload/worker/' . $CrmService->img);
            } else {
                $CrmService->img = asset('default/images/company/headpic.jpg');
            }
        }
        //电话
        if (empty($CrmService->mobile)) {
            $CrmService->mobile = Site::get('DefaultCompanyServiceMobile');
        }
        //QQ
        if (empty($CrmService->qq)) {
            $CrmService->qq = Site::get('DefaultCompanyServiceQQ');
        }
        //微信
        if (empty($CrmService->weichat)) {
            $CrmService->weichat = Site::get('DefaultCompanyServiceWeiChat');
        }
        //个人宣言
        if (empty($CrmService->enounce)) {
            $CrmService->enounce = trim(Site::get('DefaultCompanyServiceEnounce'));
        } else {
            $CrmService->enounce = trim($CrmService->enounce);
        }
        return Style::view('company.welcome', compact('companyBasic', 'companyJob', 'userInfo', 'resumeSendCount', 'endtime', 'refreshtip',
            'vipNews', 'CrmService', 'integralNumber', 'integralDeduct', 'uptime'));
    }

    /**
     * 功能：计算两个日期相差 年 月 日
     * @param date $endtime 截止日期日期
     * @return array(int,'单位')
     */
    protected function timeDiff($endtime)
    {
        $starttime = time();
        $endtime = strtotime($endtime);

        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        $secs = $remain % 60;
        if ($days > 0) return array($days, '天');
        if ($hours > 0) return array($hours, '小时');
        if ($mins > 0) return array($mins, '分种');
        return array($secs, '秒');
    }

    /**
     * 设置app下载
     * @param $endtime
     */
    protected function setApiSession(Request $request){
        session()->put('company_api',1);
    }
}